package cn.ffcs.uec.staff.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.staff.action.bean.UecRetriedEditBean;
import cn.ffcs.uec.staff.model.UecRetried;
import cn.ffcs.uec.staff.model.UecStaffInfo;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.NodeVo;

/**
 * 军退退役人员 编辑组件
 * 
 * @Title:
 * @Description:
 * @Copyright: FFCS（C） 2017
 * @Company: FFCS
 * @author Wang Yiren
 * @Version 0.0.1 date:2018年1月7日
 * @since JDK1.6
 */
@Controller
@Scope("prototype")
public class UecRetriedEditComposer extends BasePortletComposer {

	private static final long serialVersionUID = 3086340593438520480L;
	/**
	 * 页面bean
	 */
	private UecRetriedEditBean bean = new UecRetriedEditBean();
	/**
	 * 操作类型
	 */
	private String opType;
	/**
	 * 人员信息
	 */
	private UecStaffInfo uecStaffInfo;
	/**
	 * 军退退役人员
	 */
	private UecRetried uecRetried;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 初始化窗口
	 * 
	 * @throws ParseException
	 */
	public void onCreate$uecRetriedEditWindow() throws ParseException {
		this.bindCombobox();
		this.bindBean();
	}

	/**
	 * 绑定combobox
	 */
	private void bindCombobox() {

		List<NodeVo> nodeVoList = new ArrayList<NodeVo>();
		String[][] ishiskeypsn = { { "Y", "是" }, { "N", "否" } };
		for (int i = 0; i < ishiskeypsn.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(ishiskeypsn[i][j]);
				nodeVo.setName(ishiskeypsn[i][j + 1]);
			}
			nodeVoList.add(nodeVo);
		}
		// 军转干部
		ListboxUtils.rendererForEdit(bean.getGlbdef14(), nodeVoList);
		// 退役士兵
		ListboxUtils.rendererForEdit(bean.getGlbdef15(), nodeVoList);

		// 退役人员性质
		List<NodeVo> nodeVoGlbdef1List = new ArrayList<NodeVo>();
		String[][] glbdef1 = { { "1001A11000000000069N", "复退" },
				{ "1001A11000000000069O", "转业" } };
		for (int i = 0; i < glbdef1.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(glbdef1[i][j]);
				nodeVo.setName(glbdef1[i][j + 1]);
			}
			nodeVoGlbdef1List.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getGlbdef1(), nodeVoGlbdef1List);

		// 军衔文职级glbdef3
		List<NodeVo> nodeVoGlbdef3List = new ArrayList<NodeVo>();
		String[][] glbdef3 = { { "1001A110000000000600", "大校" },
				{ "1001A110000000000601", "上校" },
				{ "1001A110000000000602", "中校" },
				{ "1001A110000000000603", "少校" },
				{ "1001A110000000000604", "上尉" },
				{ "1001A110000000000605", "中尉" },
				{ "1001A110000000000606", "少尉" },
				{ "1001A110000000000607", "行政12级" },
				{ "1001A110000000000608", "行政13级" },
				{ "1001A110000000000609", "行政14级" },
				{ "1001A11000000000060A", "行政15级" },
				{ "1001A11000000000060B", "行政16级" },
				{ "1001A11000000000060C", "行政17级" },
				{ "1001A11000000000060D", "行政18级" },
				{ "1001A11000000000060E", "行政19级" },
				{ "1001A11000000000060F", "行政20级" },
				{ "1001A11000000000060G", "行政21级" },
				{ "1001A11000000000060H", "行政22级" },
				{ "1001A11000000000060I", "行政23级" },
				{ "1001A11000000000060J", "3级" },
				{ "1001A11000000000060K", "4级" },
				{ "1001A11000000000060L", "5级" },
				{ "1001A11000000000060M", "6级" },
				{ "1001A11000000000060N", "7级" },
				{ "1001A11000000000060O", "8级" },
				{ "1001A11000000000060P", "9级" },
				{ "1001A11000000000060Q", "其他" } };
		for (int i = 0; i < glbdef3.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(glbdef3[i][j]);
				nodeVo.setName(glbdef3[i][j + 1]);
			}
			nodeVoGlbdef3List.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getGlbdef3(), nodeVoGlbdef3List);

	}

	/**
	 * bindBean
	 * 
	 * @throws ParseException
	 */
	public void bindBean() throws ParseException {
		opType = (String) arg.get("opType");
		if ("add".equals(opType)) {
			this.bean.getUecRetriedEditWindow().setTitle("新增军退退役人员信息");
			bean.getUecRetriedEditDiv().setHeight("40%");
			uecStaffInfo = (UecStaffInfo) arg.get("uecStaffInfo");
		}
		if ("view".equals(opType)) {
			this.bean.getUecRetriedEditWindow().setTitle("查看军退退役人员信息");
			uecRetried = (UecRetried) arg.get("uecRetried");
			if (null != uecRetried) {
				PubUtil.fillBeanFromPo(uecRetried, bean);
				stringTransToDate();
				bean.getButtonDiv().setVisible(false);
				PubUtil.disableComponent(bean.getUecRetriedEditWindow(), true);
			}
		}
		if ("edit".equals(opType)) {
			this.bean.getUecRetriedEditWindow().setTitle("编辑军退退役人员信息");
			bean.getUecRetriedEditDiv().setHeight("40%");
			uecRetried = (UecRetried) arg.get("uecRetried");
			if (null != uecRetried) {
				PubUtil.fillBeanFromPo(uecRetried, bean);
				stringTransToDate();
			}
		}
	}

	/**
	 * 保存
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = this.checkUacStaffData();
		if (null != msg) {
			Messagebox.show(msg);
			return;
		}
		if ("add".equals(opType)) {
			uecRetried = UecRetried.newInstance();
			PubUtil.fillPoFromBean(bean, uecRetried);
			dateTransToString();
			uecRetried.setStaffId(uecStaffInfo.getStaffId());
			uecRetried.addOnly();
			Events.postEvent("onOK", this.self, uecRetried);
			this.onCancel();
			Messagebox.show("新增成功！");
		}
		if ("edit".equals(opType)) {
			PubUtil.fillPoFromBean(bean, uecRetried);
			dateTransToString();
			uecRetried.updateOnly();
			Events.postEvent("onOK", this.self, uecRetried);
			this.onCancel();
			Messagebox.show("更新成功！");
		}
	}

	/**
	 * 取消
	 */
	public void onCancel() {
		// 关闭窗口
		this.bean.getUecRetriedEditWindow().onClose();
	}

	/**
	 * 检测填入的参数是否为空
	 */
	private String checkUacStaffData() {
		if (StrUtil.isNullOrEmpty(bean.getGlbdef12().getValue())) {
			return "请填写企业政策性接收时间";
		} else if (StrUtil.isNullOrEmpty(bean.getGlbdef13().getValue())) {
			return "请填写军转退役时间";
		}
		return null;
	}

	/**
	 * 将Date类型时间转成String类型 Date --> String 一套模型，两套标准：日期类型需要格式化
	 */
	public void dateTransToString() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		// 企业政策性接收时间
		if (!StrUtil.isNullOrEmpty(bean.getGlbdef12().getValue())) {
			uecRetried.setGlbdef12(formatter.format(bean.getGlbdef12()
					.getValue()));
		}
		// 军转退役时间
		if (!StrUtil.isNullOrEmpty(bean.getGlbdef13().getValue())) {
			uecRetried.setGlbdef13(formatter.format(bean.getGlbdef13()
					.getValue()));
		}

	}

	/**
	 * 将String类型的时间转换成Date类型
	 * 
	 * @throws ParseException
	 */
	public void stringTransToDate() throws ParseException {
		// 时间类型转换：String --> Date
		// 企业政策性接收时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if (null != uecRetried.getGlbdef12()) {
			bean.getGlbdef12().setValue(sdf.parse(uecRetried.getGlbdef12()));
		}
		// 军转退役时间
		if (null != uecRetried.getGlbdef13()) {
			bean.getGlbdef13().setValue(sdf.parse(uecRetried.getGlbdef13()));
		}
	}

}
